package cc.autoapi.pucong.auto.flow.persistence.repository.properties;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.PropertiesVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.DataBus;
import cc.autoapi.pucong.auto.flow.core.root.utils.AESEncryptUtil;
import cc.autoapi.pucong.auto.flow.persistence.po.properties.PropertiesInfoPo;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.DatabaseUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pucong
 * @description 全局参数信息 service 实现类
 * @create 2024-10-06
 */
@Service
public class PropertiesInfoServiceImpl implements PropertiesInfoService {

    @Override
    public void initCache() {
        List<PropertiesInfoPo> propertiesInfoPos = list();
        for (PropertiesInfoPo propertiesInfoPo : propertiesInfoPos) {
            putCache(propertiesInfoPo);
        }
    }

    @Override
    public void putCache(PropertiesInfoPo po) {
        PropertiesVo propertiesVo = new PropertiesVo();
        propertiesVo.setUuid(po.getUuid());
        propertiesVo.setPropertiesCode(po.getPropertiesCode());
        propertiesVo.setPropertiesType(po.getPropertiesType());
        propertiesVo.setPropertiesValue(po.getPropertiesValue());
        if (po.getNeedEncryption() == 1) {
            propertiesVo.setPropertiesValue(AESEncryptUtil.decrypt(po.getPropertiesValue()));
        }
        DataBus.setProperty(propertiesVo.getPropertiesCode(), propertiesVo);
    }

    @Override
    public List<PropertiesInfoPo> list() {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_properties_info where 1=1 ");
        sqlBf.append(" order by properties_code asc ");
        List<PropertiesInfoPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), PropertiesInfoPo.class);
        return apiInfoPos;
    }

    @Override
    public List<PropertiesInfoPo> list(String groupId, String propertiesCode) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_properties_info where 1=1 ");
        if (StringUtils.isNotBlank(groupId)) {
            sqlBf.append(" and group_id = ? ");
            params.add(groupId);
        }
        if (StringUtils.isNotBlank(propertiesCode)) {
            sqlBf.append(" and properties_code = ? ");
            params.add(propertiesCode);
        }
        sqlBf.append(" order by properties_code asc ");
        List<PropertiesInfoPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), PropertiesInfoPo.class);
        return apiInfoPos;
    }

    @Override
    public PropertiesInfoPo getById(String id) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_properties_info where 1=1 and uuid = ? ");
        params.add(id);
        PropertiesInfoPo apiInstancePo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), PropertiesInfoPo.class);
        return apiInstancePo;
    }

    @Override
    public long getCountByCode(String propertiesCode) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select count(0) from auto_flow_properties_info where 1=1 and properties_code = ? ");
        params.add(propertiesCode);
        Integer count = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), Integer.class);
        return count;
    }

    @Override
    public Boolean save(PropertiesInfoPo propertiesInfo) {
        DatabaseUtils.saveAuto("auto_flow_properties_info", propertiesInfo);
        return true;
    }

    @Override
    public long getCountByCode(String id, String propertiesCode) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select count(0) from auto_flow_properties_info where 1=1 and properties_code = ?  and uuid != ?");
        params.add(propertiesCode);
        params.add(id);
        Integer count = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), Integer.class);
        return count;
    }

    @Override
    public Boolean updateById(PropertiesInfoPo propertiesInfoUpdate) {
        DatabaseUtils.updateAutoById("auto_flow_properties_info", propertiesInfoUpdate);
        return true;
    }

    @Override
    public Boolean removeBatchByIds(List<String> ids) {
        if (ids == null || ids.size() < 1) {
            return false;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_properties_info where uuid in (");
        for (int i = 0; i < ids.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(ids);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
        return true;
    }
}
